//-------------------------------------------------------------------------------------------------->
//
// Copyright 2002 e Professional Corporation.  All Rights Reserved.
//
// File:         	CAEPMU03.java
//
// Author:   	poompui
//
// Date:			02/05/2003
//
//--------------------------------------------------------------------------------------------------->
package com.epro.standard.frontend.servlet.masterutil;
//======================Import package=======================>
import java.io.IOException;

import java.sql.Timestamp;

import java.util.Date;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epro.standard.StandardException;
import com.epro.standard.frontend.datasetup.MessageManager;
import com.epro.standard.frontend.servlet.EproServlet;
import com.epro.standard.frontend.util.EproServletUtil;
import com.epro.standard.frontend.util.FileInfoUtil;
import com.epro.standard.frontend.util.StringUtil;
import com.epro.standard.usermenu.UserAccountVO;
import com.epro.standard.usermenu.UserGroupEntity;
import com.epro.standard.usermenu.UserGroupVO;
import com.epro.standard.usermenu.manage.UserGroupMange;

public class CAEPMU03 extends EproServlet {
    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, 
                                                            IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);
        String action = (servletUtil.getRequestValue("action")).toLowerCase();

        //=================Select action for Managing==================>		
        if (action.equals("insert"))
            insertData(request, response);
        else if (action.equals("update"))
            updateData(request, response);
        else if (action.equals("delete"))
            deleteData(request, response);
    }


    //-------------------------------------------------------------------------------------------------->
    //																																	\\
    //													INSERT																\\
    //																																	\\
    //--------------------------------------------------------------------------------------------------->	

    public void insertData(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);

        String updBy = 
            session.getAttribute("userLogin") == null ? MessageManager.getDoseNotSignInString() : 
            ((UserAccountVO)session.getAttribute("userLogin")).getUA_USER_NAME();
        String ugID = servletUtil.getRequestValue("ugID");
        String ugName = servletUtil.getRequestValue("ugName");
        String roleID = servletUtil.getRequestValue("roleID");
        //System.out.println("roleID : " + roleID);
        //System.out.println("ugName : " + ugName);
        //System.out.println("ugID : " + ugID);
        //System.out.println("updBy : " + updBy);

        //======================Define redirect page==================>
        String parameterLink = "";
        String URL = 
            ((FileInfoUtil)session.getAttribute("fileInfoUtil")).getRedirectFromServlet();
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;

        //======================Define Object======================>
        UserGroupMange ugManager = new UserGroupMange();
        UserGroupEntity ugEntity = new UserGroupEntity(ugID);

        try {
            //======================Set Property of entity Object===========>
            ugEntity.setUserGroupName(ugName);
            //			ugEntity.setRoleID(roleID);
            ugEntity.setUpdBy(updBy);
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            ugEntity.setUpdDate(timestamp);
            ugEntity.setActive("Y");

            //======================Set Method for Manage================>
            //System.out.println("before manage");
            ugManager.addUserGroup(ugEntity);
            //System.out.println("ending manage");

            //======================Set session for Master page Alert ========>
            session.setAttribute("hasError", 
                                 MessageManager.getInsertSuccessString());
            response.sendRedirect(successLink);
        } catch (StandardException e) {
            e.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getInsertUnsuccessString() + 
                                 StringUtil.encode2Thai(e.getDescription()));
            response.sendRedirect(unsuccessLink);
        }
    }


    //-------------------------------------------------------------------------------------------------->
    //																																	\\
    //													UPDATE																\\
    //																																	\\
    //--------------------------------------------------------------------------------------------------->		

    public void updateData(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);

        String updBy = 
            session.getAttribute("userLogin") == null ? MessageManager.getDoseNotSignInString() : 
            ((UserAccountVO)session.getAttribute("userLogin")).getUA_USER_NAME();
        String ugID = servletUtil.getRequestValue("ugID");
        String ugName = servletUtil.getRequestValue("ugName");
        String roleID = servletUtil.getRequestValue("roleID");
        String active = servletUtil.getRequestValue("active");
        //System.out.println("roleID : " + roleID);
        //System.out.println("ugName : " + ugName);
        //System.out.println("ugID : " + ugID);
        //System.out.println("active : " + active);
        //System.out.println("updBy : " + updBy);

        //======================Define redirect page==================>
        String parameterLink = "";
        String URL = 
            ((FileInfoUtil)session.getAttribute("fileInfoUtil")).getRedirectFromServlet();
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;

        //======================Define Object======================>
        UserGroupMange ugManager = new UserGroupMange();
        UserGroupEntity ugEntity = new UserGroupEntity(ugID);

        try {
            //======================Set Property of entity Object===========>
            ugEntity.setUserGroupName(ugName);
            //			ugEntity.setRoleID(roleID);
            ugEntity.setUpdBy(updBy);
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            ugEntity.setUpdDate(timestamp);
            ugEntity.setActive(active);

            //======================Set Method for Manage================>
            //System.out.println("before manage");
            ugManager.editUserGroup(ugEntity);
            //System.out.println("ending manage");

            //======================Set session for Master page Alert ========>
            session.setAttribute("hasError", 
                                 MessageManager.getUpdateSuccessString());
            response.sendRedirect(successLink);
        } catch (StandardException e) {
            e.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getUpdateUnsuccessString() + 
                                 StringUtil.encode2Thai(e.getDescription()));
            response.sendRedirect(unsuccessLink);
        }
    }


    //-------------------------------------------------------------------------------------------------->
    //																																	\\
    //													DELETE																\\
    //																																	\\
    //--------------------------------------------------------------------------------------------------->	

    public void deleteData(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        //======================Define Variable======================>
        HttpSession session = request.getSession();
        EproServletUtil servletUtil = new EproServletUtil(request);

        String userID = 
            session.getAttribute("userLogin") == null ? "Doesn't Signin" : 
            ((UserAccountVO)session.getAttribute("userLogin")).getUA_USER_ID();
        int index = servletUtil.getRequestIntValue("index");
        String chkRow[] = servletUtil.getRequestValues("chkRow");

        //======================Define redirect page==================>
        String parameterLink = "";
        String URL = 
            ((FileInfoUtil)session.getAttribute("fileInfoUtil")).getRedirectFromServlet();
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;

        //======================Define Object======================>
        UserGroupMange userGroupManager = new UserGroupMange();

        try {
            if (chkRow == null) {
                session.setAttribute("hasError", 
                                     MessageManager.getDeleteUnSelectString());
                response.sendRedirect(unsuccessLink);
            } else {
                String ar[];
                ar = new String[chkRow.length];
                ar = chkRow;
                Vector dataVector = 
                    (Vector)session.getAttribute("UserGroupManagerVector");
                boolean chkSubmit = false;
                int numE = 0, numD = 0;
                String canUpdate = "";
                String cannotUpdate = "";
                //System.out.println("before delete " + ar.length);
                for (int i = 0; i < ar.length; i++) {
                    UserGroupVO userGroupVO = 
                        (UserGroupVO)dataVector.get(Integer.parseInt(ar[i]) + 
                                                    index);
                    UserGroupEntity userGroupEntity = 
                        new UserGroupEntity(userGroupVO.getUG_USER_GROUP_ID());
                    Date date = new Date();
                    Timestamp timestamp = new Timestamp(date.getTime());
                    userGroupEntity.setUpdDate(timestamp);
                    userGroupManager.removeUserGroup(userGroupEntity);
                    numD = numD + 1;
                    chkSubmit = true;
                }
                //System.out.println("ending delete");		
                if (numD != 0)
                    session.setAttribute("hasError", 
                                         MessageManager.getDeleteSuccessALLString() + 
                                         numD + 
                                         MessageManager.getUnitString());

                if (chkSubmit)
                    response.sendRedirect(successLink);
                else {
                    session.setAttribute("hasError", 
                                         MessageManager.getDeleteUnsuccessString());
                    response.sendRedirect(unsuccessLink);
                }
            } // end if( chkRow.length == 0 )
        } catch (StandardException e) {
            e.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getDeleteUnsuccessString() + 
                                 StringUtil.encode2Thai(e.getDescription()));
            response.sendRedirect(unsuccessLink);
        }

    }
}

